package jmx.demo.server;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

import jmx.demo.mbean.HelloWorld;
/**
 * 如果大家在尝试写Agent程序时出现connection refused的异常的时候，不用怕，赶紧检查一下你的程序中是不是有这句话：
   Registry registry = LocateRegistry.createRegistry(rmiPort);
   LocateRegistry.createRegistry(int port)方法可以在某一特定端口创建名字服务,从而用户无需再手工启动rmiregistry
   或者，你也可以运行  jdkfolder/bin/rmiregistry.exe 9999
   其中jdkfolder是你的jdk的安装目录，9999是你要绑定的端口
   运行上面的命令和你在代码中添加上面那行code是一样的效果
   
 3. Agent的实现中的32行和33行是两种获取MBeanServer的方式。
    MBeanServer mbs = MBeanServerFactory.createMBeanServer(jmxServerName);
    这种方式主要用于JDK1.5以前
  MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
   这种方式是JDK1.5引入的。
   安全起见，当大家不能确定以后部署的机器上面安装的JDK是1.5以上的版本时，建议按照第一种方式。
   
 4. 绑定需要被管理的类
    ObjectName objName = new ObjectName(jmxServerName + ":name=" + "HelloWorld");
  mbs.registerMBean(new HelloWorld(), objName);
  这里我们可以看到，首先要给被绑定的类起一个名字,然后把这个名字和被管理的类一起注册到MBeanServer当中。
  
5. 将服务绑定到固定的URL上
  JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + rmiPort + "/" + jmxServerName);
  JMXConnectorServer jmxConnServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
  jmxConnServer.start();
       在start方法被调用后，服务就已经发布成功，我们就可以通过页面或者其他的方式来访问服务
 */
import com.sun.jdmk.comm.HtmlAdaptorServer;
/**
 * 
 *  Class Name: HelloWorldAgent.java
 *  Description: 关于jmx的文章详解在这里：http://blog.csdn.net/fanyingnew/article/details/1546238
 *  @author wanghao  DateTime 2017年2月7日 下午5:39:46 
 *  @company hongbo
 *  @email 1228675455@qq.com
 */
public class HelloWorldAgent {

	public static void main(String[] args) throws MalformedObjectNameException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, IOException {
		
		int rmiPort = 1089;
		String jmxServerName = "testJMXServer";
		
		Registry registry = LocateRegistry.createRegistry(rmiPort);
		MBeanServer mbs = MBeanServerFactory.createMBeanServer(jmxServerName);
		
		HtmlAdaptorServer adapter = new HtmlAdaptorServer();
		ObjectName adapterName;
		adapterName = new ObjectName(jmxServerName + ":name=" + "htmladapter");
		adapter.setPort(8082);
		adapter.start();
		mbs.registerMBean(adapter, adapterName);
		
		ObjectName objName = new ObjectName(jmxServerName + ":name=" + "HelloWorld");
		mbs.registerMBean(new HelloWorld(), objName);
		
		JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + rmiPort + "/" + jmxServerName);
		System.out.println("JMXServiceURL: " + jmxServiceURL.toString());  
		JMXConnectorServer jmxConnServer = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, null, mbs);  
	    jmxConnServer.start();  
	    
	}
}
